<template>
  <el-form ref="form" :model="form" label-width="80px">
    <!-- <el-form-item label="商品类型" size="small">
      <el-input v-model="bool"></el-input>
    </el-form-item> -->
    <!-- <el-select v-model="bool" placeholder="请选择">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select> -->
    <el-form-item label="商品名称" size="small">
      <el-input v-model="name"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" size="small">
      <el-input v-model="pirce"></el-input>
    </el-form-item>
    <el-form-item label="商品id" size="small">
      <el-input v-model="shopid"></el-input>
    </el-form-item>
    <el-form-item label="商品库存" size="small">
      <el-input v-model="stock"></el-input>
    </el-form-item>

    <!-- <el-form-item label="冻结状态">
    <el-switch v-model="form.delivery"></el-switch>
    </el-form-item>-->
    <el-form-item label="售卖类型" size="small">
      <el-radio-group v-model="bool">
      <el-radio label="鲜花" value='鲜花'></el-radio>
      <el-radio label="仿真花" value='仿真花'></el-radio>
      <el-radio label="绿色植物" value='绿色植物'></el-radio>
    </el-radio-group>
    </el-form-item>
    <el-form-item label="图片上传">
      <el-upload
        :auto-upload="true"
        action="http://localhost:3000/admins/addImage"
        list-type="picture-card"
        :on-success="uploadSuccess"
        :on-preview="handlePictureCardPreview"
        :on-remove="handleRemove"
      >
        <i slot="default" class="el-icon-plus"></i>
      </el-upload>
      <img :src="dialogImageUrl" alt />
      <el-button style="margin: 20px;" size="medium" type="success" round @click="addAdmins">确认新增</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import axios from "axios";
import { request } from "../../network/request";
import { createNamespacedHelpers } from "vuex";
const { mapState, mapActions } = createNamespacedHelpers("admin");
export default {
  data() {
    return {
      imageUrl: "",
      name: "",
      pirce: "",
      shopid: "",
      stock: "",
      bool: "",
      imgUrl:'',
      form: {
        name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: ""
      },
      dialogImageUrl: "",
      dialogVisible: false,
         options: [{
          value: '选项1',
          label: '鲜花'
        }, {
          value: '选项2',
          label: '绿色植物'
        }, {
          value: '选项3',
          label: '仿真花'
        }],
        value: ''
    };
  },
 
  methods: {
    ...mapActions(["addAdmin"]),

    uploadSuccess(file, fileList) {
      // console.log(file);
      console.log(file.filename[0]);
      this.imgUrl = file.filename[0];
      // console.log(this.imgUrl);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    addAdmins() {
      // console.log(this.dialogImageUrl);
      this.addAdmin({
        name: this.name,
        bool: this.bool,
        shopid: this.shopid,
        pirce: this.pirce,
        shopid: this.shopid,
        stock: this.stock,
        img:this.imgUrl
      });
      this.$router.push('/system/admin')
    }
  },
  created() {}
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 95px;
  height: 95px;
  line-height: 95px;
  text-align: center;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
}
.avatar {
  width: 95px;
  height: 95px;
}
</style>